import axios from "axios";
import { createApp } from "vue";
import App from "./App.vue";
import { createRouter, createWebHashHistory } from "vue-router";
import "element-plus/theme-chalk/index.css";
// 图标和组件需要分开引入
import ElementPlus from "element-plus";
import "./assets/css/public.less";
import utils from './utils/index.js'
// import 'amfe-flexible'
// 我们后面再讨论嵌套路由。
const routes = [
  {
    path: "/",
    name: "",
    component: () => import("@/view/pc/login"),
  },
  {
    path: "/register",
    name: "register",
    component: () => import("@/view/pc/register"),
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/view/pc/login"),
  },
  {
    path: "/pc",
    name: "pc",
    component: () => import("@/view/pc/index"),
    children: [
      {
        path: "fuh_detail",
        name: "fuh_detail",
        component: () => import("@/view/pc/fuh_detail"),
      },
      {
        path: "fuh_list",
        name: "fuh_list",
        component: () => import("@/view/pc/fuh_list"),
      },
      {
        path: "fuh_saisxz",
        name: "fuh_saisxz",
        component: () => import("@/view/pc/fuh_saisxz"),
      },
      {
        path: "jinjqr_detail",
        name: "jinjqr_detail",
        component: () => import("@/view/pc/jinjqr_detail"),
      },
      {
        path: "jinjqr_list",
        name: "jinjqr_list",
        component: () => import("@/view/pc/jinjqr_list"),
      },
      {
        path: "jinjqr_saisxz",
        name: "jinjqr_saisxz",
        component: () => import("@/view/pc/jinjqr_saisxz"),
      },
      {
        path: "zilsh_detail",
        name: "zilsh_detail",
        component: () => import("@/view/pc/zilsh_detail"),
      },
      {
        path: "zilsh_list",
        name: "zilsh_list",
        component: () => import("@/view/pc/zilsh_list"),
      },
      {
        path: "zilsh_saisxz",
        name: "zilsh_saisxz",
        component: () => import("@/view/pc/zilsh_saisxz"),
      },
      {
        path: "saisps_saisxz",
        name: "saisps_saisxz",
        component: () => import("@/view/pc/saisps_saisxz"),
      },
      {
        path: "canszp_saisxz",
        name: "canszp_saisxz",
        component: () => import("@/view/pc/canszp_saisxz"),
      },
      {
        path: "pings",
        name: "pings",
        component: () => import("@/view/pc/pings"),
      },
      {
        path: "zuopxq",
        name: "zuopxq",
        component: () => import("@/view/pc/zuopxq"),
      },
      {
        path: "cansgc",
        name: "cansgc",
        component: () => import("@/view/pc/cansgc"),
      },
      {
        path: "list",
        name: "list",
        component: () => import("@/view/pc/list"),
      },
      {
        path: "home",
        name: "home",
        component: () => import("@/view/pc/home"),
      },
      {
        path: "project2",
        name: "project2",
        component: () => import("@/view/pc/project2"),
      },
      {
        path: "project",
        name: "project",
        component: () => import("@/view/pc/project"),
      },
      {
        path: "myWork",
        name: "myWork",
        component: () => import("@/view/pc/myWork"),
      },
      {
        path: "review",
        name: "review",
        component: () => import("@/view/pc/review"),
      },
      {
        path: "reviewSubmit",
        name: "reviewSubmit",
        component: () => import("@/view/pc/reviewSubmit"),
      },
    ],
  },
];

// 3. 创建路由实例并传递 `routes` 配置
// 你可以在这里输入更多的配置，但我们在这里
// 暂时保持简单
const router = createRouter({
  // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
  history: createWebHashHistory(),
  routes, // `routes: routes` 的缩写
});

// 添加请求拦截器
axios.interceptors.request.use(
  function (config) {
    // 在发送请求之前做些什么
    console.log("请求拦截器 - 请求发送前");
    return config;
  },
  function (error) {
    // 对请求错误做些什么
    console.error("请求拦截器 - 请求发送错误", error);
    return Promise.reject(error);
  }
);

// 添加响应拦截器
axios.interceptors.response.use(
  function (response) {
    // 对响应数据做点什么
    return response;
  },
  function (error) {
    // 对响应错误做点什么
    if (error.response.status === 401) {
      // 如果是 401 错误，跳转到登录页面
      router.push("/login");
    }
    
    return Promise.reject(error);
  }
);
import zhCn from 'element-plus/es/locale/lang/zh-cn'
// 5. 创建并挂载根实例
const app = createApp(App);
//确保 _use_ 路由实例使
//整个应用支持路由。
app.use(router);
app.use(ElementPlus,{locale: zhCn});

// 全局变量

app.provide("imageUrl","http://121.42.239.184/zhaom/jeecg-boot/sys/common/static/");
app.provide("serviceUrl", "http://121.42.239.184/zhaom/jeecg-boot");
// app.provide("serviceUrl", "http://121.42.239.184:7001/zhaom2/jeecg-boot");

app.provide(
  "imageUrl",
  "http://MS-YDFKBCLJMPNU:8080/jeecg-boot/sys/common/static/"
);
app.provide("serviceUrl", "http://MS-YDFKBCLJMPNU:8080/jeecg-boot");

import "viewerjs/dist/viewer.css"
import Viewer from "v-viewer"

app.use(Viewer)
app.config.productionTip = false;
app.config.globalProperties.$utils = utils
app.mount("#app");

// 现在，应用已经启动了！

// 解决ERROR ResizeObserver loop completed with undelivered notifications.
const debounce = (fn, delay) => {
  let timer = null;

  return function () {
    let context = this;
    let args = arguments;
    clearTimeout(timer);
    timer = setTimeout(function () {
      fn.apply(context, args);
    }, delay);
  };
};

const _ResizeObserver = window.ResizeObserver;
window.ResizeObserver = class ResizeObserver extends _ResizeObserver {
  constructor(callback) {
    callback = debounce(callback, 16);
    super(callback);
  }
};
